package com.xiubai.appforpda.jack.utils;

import com.google.myjson.Gson;
import com.google.myjson.JsonSyntaxException;

import java.lang.reflect.Type;
import java.util.ArrayList;

public class JsonParser {
    public static Gson gson = new Gson();

    /**
     * 返回json转换后的对象.
     */
    public static <T> T deserializeByJson(String data, Type type) {
        return gson.fromJson(data, type);
    }

    /**
     * 返回json转换后的对象.
     */
    public static <T> T deserializeByJson(String data, Class<T> clz) {
        try {
            T t = gson.fromJson(data, clz);
            return t;
        } catch (JsonSyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    public static <T> ArrayList<T> deserializeByJsonToList(String json, Type listType) {
        //        Type listType = new TypeToken<ArrayList<TestT>>() {
//        }.getType();
//        Gson gson = new Gson();
//        final List<TestT> list = gson.fromJson(json, listType);
        Gson gson = new Gson();
        return gson.fromJson(json, listType);
    }

    /**
     * 返回json字符�?
     */
    public static <T> String serializeToJson(T t) {
        return gson.toJson(t);
    }

}